package com.cms.doc.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cms.doc.model.DocText;
import com.cms.doc.model.DocType;
import com.cms.doc.service.DocTextService;
import com.cms.doc.service.DocTypeService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.DicCache;
import com.jframe.sys.controller.utils.JsBuilder;
import com.jframe.sys.utils.CalendarUtil;
import com.jframe.sys.utils.FileUtil;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;

public class DocTextAction extends ExtendAction {
	
	private static String ACTION="docTextAction";
	private static String SERVICE = "docTextService";
	private static Class<DocText> CLAZZ = DocText.class;
	
	public void list(){
		List<Columns> frozenColumns = new ArrayList<Columns>();// 固定列
		List<Columns> commonColumns = new ArrayList<Columns>();// 活动列
		
		frozenColumns.add(new Columns("ck", "", 40, ""));
		//frozenColumns.add(new Columns("id", "编号", 100, ""));
		if(!this.pageInfo.getPageTabId(request).equals("3")){
			frozenColumns.add(new Columns("操作", 120, "operation"));
		}
		frozenColumns.add(new Columns("title", "标题", 320, ""));
		//commonColumns.add(new Columns("shortTitle", "副标题", 100, ""));
		commonColumns.add(new Columns("author", "作者", 120, ""));
		frozenColumns.add(new Columns("typeId", "文章类别", 120, new Editor("combobox",true),DicCache.getComboboxJson("sm_cms_docType")));
		commonColumns.add(new Columns("pubTime", "发布日期", 120, ""));
		commonColumns.add(new Columns("pubEndTime", "归档日期", 120, ""));
		commonColumns.add(new Columns("updateTime", "修改时间", 120, ""));
		commonColumns.add(new Columns("pubUser", "文章发布人", 120, ""));
		commonColumns.add(new Columns("visitNum", "访问量", 120, ""));
		commonColumns.add(new Columns("status", "文章状态", 120, new Editor("combobox",true),DicCache.getComboboxJson("dm_cms_docStatus")));
		
		Grid grid = new Grid();
		grid.setUrl(getUrl(ACTION+".do?method=load"));
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/cms/doc/list/docTextList.jsp");
	}
	
	public void load(){
		baseLoad(SERVICE, CLAZZ," order by isTop asc,updateTime desc");
	}
	
	public void save() {
		baseSave(SERVICE, CLAZZ);
	}
	
	public Object beforeSave(Object obj){
		DocText docText = (DocText)obj;
		String content = request.getParameter("content");		
		DocTypeService docTypeService = (DocTypeService)SpringUtil.getBean("docTypeService");
		DocType docType = docTypeService.load(docText.getTypeId());
		String url = docText.getDocUrl();
		if(StringUtil.null2Space(url).equals("")){
			String fileName = FileUtil.getRandomFileName("html");
			url = "upload/cms/doc/"+ docType.getIndexUrl() + "/"+ fileName;
			docText.setDocUrl(url);
		}
		FileUtil.writeFile(StringUtil.getPrjAbsolutePath()+url, content);
		docText.setContent("");	
		docText.setPubUser(this.getCurUser().getUserName());
		docText.setUpdateTime(CalendarUtil.getDateTime());
		if(docText.getId()==0){
			docText.setStatus("1");
		}
		return docText;
	}
	
	public void afterSave(Object obj){
		//建立索引,暂未实现
	}

	public void delete() {
		baseDelete(SERVICE);
	}

	public void getObjById() {
		Map<String,Object> map = new HashMap<String,Object>();
		int id=StringUtil.parseInt(request.getParameter("id"));
		DocText docText = null;
		String content = "";
		if(id>0){
			docText = (DocText)this.getObject(SERVICE);
			content = FileUtil.readFile(StringUtil.getPrjAbsolutePath()+docText.getDocUrl());	
			if(StringUtil.null2Space(docText.getLeadImg()).equals("")){
				docText.setLeadImg("/images/noimg.jpg");
			}
		}else{
			docText = new DocText();
			docText.setPubTime(CalendarUtil.getDate());
			docText.setPubEndTime(CalendarUtil.getChangeDate(docText.getPubTime(), "Y", 30));
			docText.setLeadImg("/images/noimg.jpg");
		}
		map.put("content", content);
		map.put("obj", docText);
		print(JsonUtil.obj2Json(map));
	}
	
	public void toTop(){
		int id = StringUtil.parseInt(request.getParameter("id"));
		String isTop = request.getParameter("status");
		DocTextService service = (DocTextService)SpringUtil.getBean(SERVICE);
		service.toTop(id, isTop);
		if(isTop.equals("1")){
			this.print(true);
		}else{
			this.print(false);
		}		
	}
	
	public void setStatus(){
		String ids = request.getParameter("ids");
		String status = request.getParameter("status");
		DocTextService service = (DocTextService)SpringUtil.getBean(SERVICE);
		service.setStatus(ids, status);
		this.print(true);
	}
}
